var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'opbal_detail_mis.html', controller: ExpenseMisCtrl}).
     otherwise({redirectTo: '/'});
}]);


function ExpenseMisCtrl($scope, $http,$window) 
   {
    $scope.currentPage = 0;
    $scope.pageSize = 50;
 	$scope.Math = window.Math;
  
	$scope.venarr=[];
	 $http.get('opbal_detail_mis_data.php?q=2').success(function(data) {
		$scope.venarr = data;						
	});
	$scope.ttypearr=[];
	 $http.get('opbal_detail_mis_data.php?q=3').success(function(data) {
		$scope.ttypearr = data;						
	});
    $scope.numberOfPages=function()
    {
        return Math.ceil($scope.data.length / $scope.pageSize);                
    };
 
	$http.get('opbal_detail_mis_data.php?q=1').success(function(data) {
    $scope.opbalmastmis = data;
    $scope.opbalmastmis2 = data;
    });
	
  	
	 $scope.total_amtpaid = function() {
        var total = 0;
        angular.forEach($scope.opbalmastmis, function(item) {
         	total += +item.amtpaid;
        })
        return total;
	} 
	
		$scope.total_amtreceived = function() {
        var totalrec = 0;
        angular.forEach($scope.opbalmastmis, function(item) {
         	totalrec += +item.amtreceived;
        })
        return totalrec;
	} 
	
	$scope.dfilter = function(from,to,ven,ttype) 
	{				
		//alert(from+":"+to+":"+markers.length);
		markers=$scope.opbalmastmis2;
		var retMarkers = [];		
		var j=parseInt(0);
		
		if(!from)
			from='0000-00-00';
		
		if(!to)
		{
			var d = new Date();					
			var d2 = d.getFullYear()+"-"+(parseInt(d.getMonth())+1)+"-"+d.getDate();			
			to=d2;
		}
		if(!ven)
			ven='';
		if(!ttype)
			ttype='';
		$('button[data-loading-text]').button('loading');
		setTimeout(function () {
			var sql='opbal_detail_mis_data.php?q=1&from='+from+'&to='+to+'&ven='+ven+'&ttype='+ttype;
		   $http.get(sql).success(function(data) {
			$scope.opbalmastmis = data;	
			//location.href=sql;			
			$('button[data-loading-text]').button('reset');
			});
		}, 2000);
		
		$scope.venarr=[];
	 $http.get('opbal_detail_mis_data.php?q=2').success(function(data) {
		$scope.venarr = data;						
	});
	$scope.ttypearr=[];
	 $http.get('opbal_detail_mis_data.php?q=3').success(function(data) {
		$scope.ttypearr = data;						
	});
	
		/*//alert(from+":"+to+":"+markers.length);
		//alert(from<markers[0].Date);
        for(var i = 0, len = markers.length; i < len; i++) 
		{
			var singleMarker = markers[i];
			if(singleMarker.opbaldate >= from && singleMarker.opbaldate <= to)
			{			
				//retMarkers.push(singleMarker);
				retMarkers[j]=singleMarker;
				//alert(retMarkers[j].exptranid);   
				j++;				    
			}
		}
		//alert($scope.opbalmastmis.length);
		$scope.opbalmastmis = retMarkers;
		$scope.opbalmastmis.length = j;*/
		
	}
/* 
	$scope.cancel = function() 
	{
		//location.reload(true);
		$scope.opbalmastmis=$scope.opbalmastmis2;
	}
    */
}
	
	  
	    
	
//We already have a limitTo filter built-in to angular,
//let's make a startFrom filter
app.filter('startFrom', function() {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    };
});


